using System;
using System.Web;
using System.Web.Mvc;
using System.Threading;
using System.Globalization;
using CookieHelper = eBenefits.Web.Helpers.CookieHelper;
using Settings = eBenefits.Web.Helpers.SettingsHelper;

namespace eBenefits.Web.Framework
{
	public class eBenefitsWebController : System.Web.Mvc.Controller
	{
		public string PreferredCulture
		{
			get
			{
				if (Session[Settings.CultureSessionName] == null)
				{
					Session[Settings.CultureSessionName] = Settings.DefaultCultureName;
					Thread.CurrentThread.CurrentUICulture = new CultureInfo (Session[Settings.CultureSessionName].ToString ());
				}

				return Session[Settings.CultureSessionName].ToString();
			}
		}
		
		public string PreferredTheme
		{
			get 
			{
				if (Session[Settings.ThemeSessionName] == null) 
				{
					Session[Settings.ThemeSessionName] = Settings.DefaultThemeName;
				}
				
				return Session[Settings.ThemeSessionName].ToString();
			}
		}
		
		public string SiteMaster
		{
			get
			{
				//return View ("Index","~/Content/Themes/NewHorizon11/Layout.master");
				string themePath = System.IO.Path.Combine(Settings.ThemeFolder, this.PreferredTheme);
				string layoutPath = System.IO.Path.Combine(themePath, "Layout.master");
				return layoutPath;
			}
		}
		
		public eBenefitsWebController ()
		{
		}
		
		protected override void OnException (ExceptionContext filterContext)
		{
			base.OnException (filterContext);
		}
		
		protected override void HandleUnknownAction (string actionName)
		{
			base.HandleUnknownAction (actionName);
		}
		
		protected override void OnActionExecuting (ActionExecutingContext filterContext)
		{
			base.OnActionExecuting (filterContext);
			// Check for user authentication here
			// Check for user authorization here
			
		}
		
		/// <summary>
		/// Set culture
		/// Set theme
		/// </summary>
		/// <param name="filterContext">
		/// A <see cref="Web.Mvc.ActionExecutedContext"/>
		/// </param>
		protected override void OnActionExecuted (ActionExecutedContext filterContext)
		{
			base.OnActionExecuted (filterContext);
			
			ViewResult result = filterContext.Result as ViewResult;
			if (result == null)
				return;
			
			//HttpRequestBase request = filterContext.HttpContext.Request;
			// Set Culture
			SetCultureCookie ();
			
			// Set Theme
			SetThemeCookie ();
			
			// TODO: Expose hooks (delegates) for other events similar to SetCultureCookie and SetThemeCookie
		}
		
		private void SetCultureCookie ()
		{
			CookieHelper.SetCookie (this.Response.Cookies, Settings.CultureCookieName, this.PreferredCulture);
		}
		
		private void SetThemeCookie ()
		{
			CookieHelper.SetCookie (this.Response.Cookies, Settings.ThemeCookieName, this.PreferredTheme);
		}
		
		public void DebugLog(string message)
		{
			string filePath = "/home/zhixian/projects/eBenefits/eBenefits.Web/App_Data/Logs/Log.txt";
			// HttpContext.Server.MapPath(Settings.LogsFolder);
			
			using (System.IO.StreamWriter sw = new System.IO.StreamWriter(filePath, true))
			{
				sw.AutoFlush = true;
				sw.WriteLine(message);
			}
		}
	}
}

